package com.ssau.tnp.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Matrix {

    private int rows, columns;
    private double[][] matrix = null;

    public Matrix(int rows, int column) {
	matrix = new double[rows][column];
	this.rows = rows;
	this.columns = column;
    }

    public int getRows() {
	return rows;
    }

    public void setRows(int rows) {
	this.rows = rows;
    }

    public int getColumns() {
	return columns;
    }

    public void setColumns(int columns) {
	this.columns = columns;
    }

    public double[][] getMatrix() {
	return matrix;
    }

    public void setMatrix(double[][] matrix) {
	this.matrix = matrix;
    }

    public double getElement(int row, int column) {
	return matrix[row][column];
    }

    public void setElement(int row, int column, double newValue) {
	matrix[row][column] = newValue;
    }

    public void writeInFile(String fileName) throws IOException {
	OutputStream f = new FileOutputStream(fileName);
	OutputStreamWriter outputStreamWriter = new OutputStreamWriter(f);
	BufferedWriter writer = new BufferedWriter(outputStreamWriter);
	writer.write(rows);
	writer.write(columns);
	for (int i = 0; i < rows; i++) {
	    for (int j = 0; j < columns; j++) {
		writer.write(Double.toString(matrix[i][j]));
	    }
	}
    }

    public void readFromFile(String fileNmame) throws IOException {
	InputStream f = new FileInputStream(fileNmame);
	InputStreamReader inputStreamReader = new InputStreamReader(f);
	BufferedReader reader = new BufferedReader(inputStreamReader);
	DataInputStream dis = new DataInputStream(new FileInputStream(new File(fileNmame)));
	rows = dis.read();
	columns = dis.read();
	int str;
	for (int i = 0; i < rows; i++) {
	    for (int j = 0; j < columns; j++) {
		matrix[i][j] = dis.read();
		System.out.println(matrix[i][j]);
	    }
	}
	dis.close();
    }
    public Matrix operation(Matrix m){
	Matrix resultMatrix = new Matrix(rows, columns);
	for (int i =0; i < rows; i++) {
	    for (int j = 0; j < columns; j++){
		resultMatrix.setElement(i, j, getElement(i, j)+m.getElement(i, j));
	    }
	}
	return resultMatrix;
    }
}
